import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_flux/flutter_flux.dart';
import 'package:freemusic/components/Common.dart';
import 'package:freemusic/components/Player.dart';
import 'package:freemusic/page/home/Function.dart';
import 'package:freemusic/page/home/Search.dart';
import 'package:freemusic/page/home/SongSheet.dart';
import 'package:freemusic/services/GedanServices.dart';
import 'package:freemusic/services/SongServices.dart';
import 'package:freemusic/utils/Util.dart';
import 'package:freemusic/utils/request/Request.dart';
import 'package:marquee/marquee.dart';
import 'package:package_info/package_info.dart';
import 'package:url_launcher/url_launcher.dart';

import 'Drawer.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }
}

class HomePageState extends State<HomePage> with StoreWatcherMixin<HomePage> {
  @override
  void initState() {
    super.initState();
    _init();
    _checkVersion();
  }

  

  void _init() {
    GedanServices gedanServices = GedanServices.getInstace();
    SongServices songServices = SongServices.getInstace();
    gedanServices.loadGedanList();
    songServices.loadTempSongList();
  }

  void _checkVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String currentVersion = packageInfo.version;
    Map remoteVersionInfo = await loadNetVersion();
    String remoteVersion = remoteVersionInfo['version'];
    String remoteContent = remoteVersionInfo['content'];
    if (currentVersion != remoteVersion) {
      showCommonDialog(context,
          barrierDismissible: false,
          title: '升级内容',
          content: remoteContent,
          onCancel: (){
            exit(0);
          },
          onConfirm: () {
            launch(remoteVersionInfo['downloadurl']);
          });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(child: DrawerPage()),
      body: Container(
        margin: EdgeInsets.only(left: 10, right: 10),
        child: Column(
          children: <Widget>[
            SearchWidget(),
            FunctionWidget(),
            Expanded(
              child: SongSheetWidget(),
            ),
            Container(
              child: Row(
                children: <Widget>[
                  Container(
                    child: Image.asset(
                      "images/laba.png",
                      width: 24,
                    ),
                    margin: EdgeInsets.only(right: 2.0),
                  ),
                  Expanded(
                    child: Container(
                      child: Marquee(
                        text: "本软件仅供学习使用，请在24小时内删除，同时请尊重音乐版权，下载正版APP，谢谢！",
                        blankSpace: 80.0,
                        velocity: 20.0,
                        pauseAfterRound: Duration(seconds: 1),
                        style: TextStyle(color: Util.wordColor),
                      ),
                      height: 20,
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: PlayerWidget(),
    );
  }
}
